<?php
namespace zhtfrontend\components\services\message;

use common\components\helpers\Fun;
use common\models\datas\CustomerBranchaccount;
use common\models\datas\Student;
use yii\base\Action;
use yii\base\ActionEvent;
use yii\helpers\ArrayHelper;
use zhtfrontend\models\datas\Admin;
use zhtfrontend\models\datas\GoodsAttr;
use zhtfrontend\models\datas\GoodsTeacher;
use zhtfrontend\models\datas\Orders;
use zhtfrontend\models\datas\OrdersItems;
use zhtfrontend\models\datas\Teacher;

class ProceduresAction extends Action
{
    public $saleman_name = '无';

    public $event = 'proceduressend';

    public $behaviorsData = [];


    public function run($order_id= null,$receiver_type ='class_teacher')
    {
           $order = Orders::findOne($order_id);
           $item = OrdersItems::find()->where(['orders_id'=>$order_id,'teach_type'=>1])->one();
           $customer_account = CustomerBranchaccount::find()->where(['site_id'=>Fun::getSiteId(),'customer_id'=>$order->customer_id])->one();
           $res = [];

           //$teacherGoods = GoodsAttr::find()->where(['goods_id'=>$item->goods_id,'obj_type'=>'teacher'])->one();
           $teacherIds = GoodsTeacher::find()->select('teacher_id')->where(['goods_id'=>$item->goods_id])->column();
           $teacher = Teacher::findOne($teacherIds[0]);
           $where = ['teacher_id'=>$teacher->parent_id,'role'=>'class_teacher'];
           if($receiver_type == 'teache_teacher'){
               $this->event = 'proceduressends';
               $where = ['teacher_id'=>$teacherIds,'role'=>'teache_teacher'];
           }
           if($receiver_type == 'customer'){
               $this->event = 'customeressend';
           }
           $adminIds = Admin::find()->select('id')->where($where)->column();
           $student = Student::find()->where(['customer_id'=>$order->customer_id])->one();
           $res['student_name'] = $student->name; //接收者id
           $res['coures_name'] = $item->name; //课程名称
           $res['admin_id'] = $adminIds; //接收者id
           $res['customer_id'] = $order->customer_id;
           $res['use_scholarship_available'] = $order->scholarship_money;
           $res['use_money'] = $order->account_money;
           $res['money'] = $customer_account->cash;
           $res['scholarship_available'] = $customer_account->scholarship_available;
           $res['href'] =\Yii::$app->params['zhtwechat_url'].'/message/index';
           $res['updated_at'] =  \Yii::$app->formatter->asDatetime($order->pay_time); //办理时间
           $data['status'] = 'ok';
           $this->sendSysmsg($res,$this->event);//触发事件

           return json_encode($data);

    }

    //调用此方法触发发送短信的事件
    public function sendSysmsg(array $res,$event){
        $this->trigger($event, new ActionEvent(["data"=>$res]));
    }

    //重写行为
    public function behaviors()
    {
        return ArrayHelper::merge(parent::behaviors(), $this->behaviorsData);
    }


}